package com.szsfm.ffmpegtest;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Environment;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.szsfm.ffmpegtest.live.LivePusher;
import com.szsfm.ffmpegtest.live.LiveStateChangeListener;

import java.io.File;

public class MainActivity extends Activity implements View.OnClickListener {

    String dirStr = Environment.getExternalStorageDirectory() + "/yinpinceshi/";
    Button button;
    TextView mTextView;
    FFMpegUtil util;
    private VideoView videoView;
    ArrayAdapter<String> adapter;
    Dialog dialog;

   // static final String URL = "rtmp://120.79.44.57:1935/live/root";

    static final String URL = "rtmp://send1a.douyu.com/live/4224151rsXdpaaRM?wsSecret=5a71404a340207b2f45bdcf3b9156db2&wsTime=5a703360&wsSeek=off";

    private LivePusher live;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        util = new FFMpegUtil();
//        button = findViewById(R.id.encode_button);
//        button.setOnClickListener(this);
//        mTextView = findViewById(R.id.text);
//        videoView = findViewById(R.id.video_view);
//
//        View customView = LayoutInflater.from(this).inflate(R.layout.action_bar_view, null);
//        ListView listView = customView.findViewById(R.id.list_view);
//
//        File dir = new File(dirStr);
//        File[] fs = dir.listFiles();
//
//        List<String> lst = new ArrayList<>();
//        for (int i = 0; i < fs.length; i++) {
//            lst.add(fs[i].getName());
//        }
//        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, lst);
//        listView.setAdapter(adapter);
//        listView.setOnItemClickListener(onItemClickListener);
//        dialog = new Dialog(this);
//        dialog.addContentView(customView, new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
//                .LayoutParams.MATCH_PARENT));
//        dialog.show();
//        adapter.notifyDataSetChanged();

        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface);
        //相机图像的预览
        live = new LivePusher(surfaceView.getHolder());

    }

    /**
     * 开始直播
     * @param
     */
    public void mStartLive(View view) {
        Button btn = (Button)view;
        if(btn.getText().equals("开始直播")){
            live.startPush(URL, new LiveStateChangeListener() {
                @Override
                public void onError(int code) {

                }
            });
            btn.setText("停止直播");
        }else{
            live.stopPush();
            btn.setText("开始直播");
        }
    }

    /**
     * 切换摄像头
     * @param btn
     */
    public void mSwitchCamera(View btn) {
        live.switchCamera();
    }

    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            dialog.dismiss();
            dialog.cancel();
            final File f = new File(dirStr, adapter.getItem(position));
            final Surface surface = videoView.getHolder().getSurface();

            util.player(f.getAbsolutePath(), surface);

        }
    };


    @Override
    public void onClick(View v) {
        if (v == button) {
            dialog.show();
        }
    }


}
